package co.fitstart.mobile.web.controller.profile;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.support.handler.FileUploadHanlder;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.utils.StringUtils;
import com.demo2do.core.web.support.HttpResult;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.user.User;
import co.fitstart.entity.user.distributor.Distributor;
import co.fitstart.mobile.service.DistributorService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/distributor-profile")
public class DistributorProfileController {
    
    @Autowired
    private DistributorService distributorService;
    
    @Autowired
    private FileUploadHanlder fileUploadHanlder;
    
    @RequestMapping(value = "", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView indexPage(@Secure User user,
                                  HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("profile/distributor-profile");
        
        Distributor distributor = distributorService.load(user);
        
        if(distributor == null) {
            modelAndView.setViewName("profile/distributor-profile-error");
            return modelAndView;
        }
        
        String path = WebUtils.getRealServerPath(request);
        
        String qrcodeUrl = StringUtils.connect(path, "?distributorId=", distributor.getId().toString());
        
        Result result = fileUploadHanlder.generateQrCode(qrcodeUrl);
        
        if(result.isValid()) {
            
            String qrcode = result.getData().get("key").toString();
            distributorService.saveQrcode(distributor, qrcode);
            
        } else {
            modelAndView.setViewName("profile/distributor-profile-qrcode-error");
            return modelAndView;
        }
        
        modelAndView.addObject("distributor", distributor);
        
        
        
        return modelAndView;
    }
    
    public HttpResult bindUser(@RequestParam(name = "distributorId") Long distributorId,
                               @RequestParam(name = "identity") String identity) {
        
        HttpResult result = new HttpResult();
        
        try {
            
            result.success().message("绑定用户信息成功");
            
        } catch (Exception e) {
            
            result.fail().message("绑定用户信息失败");
        }
        
        return result;
    }
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/account", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String editAccount(@Secure User user) {
        
        Result result = new Result();
        
        try {
            
            Distributor distributor = distributorService.load(user);
            
            result.success().data(distributor.accountDescribe());
            
        } catch (Exception e) {
            
            result.fail().message("获取账户信息失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param user
     * @param account
     * @param realName
     * @return
     */
    @RequestMapping(value = "/account", method = RequestMethod.PATCH)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String onEditAccount(@Secure User user,
                                              @RequestParam(name = "wechatId", required = false) String wechatId,
                                              @RequestParam(name = "account", required = false) String account,
                                              @RequestParam(name = "realName", required = false) String realName) {
        
        Result result = new Result();
        
        try {
            
            Distributor distributor = distributorService.load(user);
            distributorService.editAccount(distributor, wechatId, account, realName);
            
            result.success().message("编辑账户信息成功");
            
        } catch (Exception e) {
            
            result.fail().message("编辑账户信息失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/withdraw", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String withdraw(@Secure User user) {
        
        Result result = new Result();
        
        try {
            
            Distributor distributor = distributorService.load(user);
            
            result.success().data(distributor.withdrawDescribe());
            
        } catch (Exception e) {
            
            result.fail().message("获取提现信息失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param user
     * @param offer
     * @return
     */
    @RequestMapping(value = "/withdraw", method = RequestMethod.PATCH)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String onWithdraw(@Secure User user,
                                           @RequestParam("offer") BigDecimal offer) {
        
        Result result = new Result();
        
        try {
            
            Distributor distributor = distributorService.load(user);
            
            distributorService.withdraw(distributor, offer);
            
            result.success().message("提现申请提交成功");
            
        } catch (Exception e) {
            
            result.fail().message("提现申请提交失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
}
